package com.eshop.logging.tools.listener;

import org.apache.logging.log4j.LogManager;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationContext;
import com.zhonghui.license.LicenseVerify;
import java.io.File;
import com.zhonghui.license.LicenseVerifyParam;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.ApplicationListener;

@Component
public class LicenseCheckListener implements ApplicationListener<ContextRefreshedEvent>
{
    private static Logger logger;
    private String subject;
    private String publicAlias;
    private String storePass;
    private String licensePath;
    
    public LicenseCheckListener() {
        this.subject = "zhonghui_license";
        this.publicAlias = "publicCert";
        this.storePass = "zhonghui_private@123";
        this.licensePath = "zhonghui";
    }
    
    public void onApplicationEvent(final ContextRefreshedEvent event) {
        final ApplicationContext context = event.getApplicationContext().getParent();
        if (context == null) {
            final LicenseVerifyParam param = new LicenseVerifyParam();
            param.setSubject(this.subject);
            param.setPublicAlias(this.publicAlias);
            param.setStorePass(this.storePass);
            param.setLicensePath(System.getProperty("user.dir") + File.separator + this.licensePath + ".lic");
            param.setPublicKeysStorePath(System.getProperty("user.dir") + File.separator + "publicCerts");
            param.setLicensePathPro(System.getenv("CATALINA_HOME") + File.separator + "bin" + File.separator + this.licensePath + ".lic");
            final LicenseVerify licenseVerify = new LicenseVerify();
            licenseVerify.install(param);
        }
    }
    
    static {
        LicenseCheckListener.logger = LogManager.getLogger((Class)LicenseCheckListener.class);
    }
}
